#include "hal_data.h"

#include "dsi_layer.h"
#include "lvgl.h"
#include "lv_demos.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER


#define RGB_565_REG    (0x1F << 11)
#define RGB_565_GREEN  (0x3F << 5)
#define RGB_565_BLUE   (0x1F << 0)

/* Global variable to keep track of requested application */
void SysTick_Handler(void);

#define LVGL_TICK_MS 1U
static volatile uint32_t s_tick        = 0U;
static volatile bool s_lvglTaskPending = false;
#define LVGL_TASK_PERIOD_TICK 3U
static void DEMO_SetupTick(void)
{
    if (0 != SysTick_Config(SystemCoreClock / (LVGL_TICK_MS * 1000U)))
    {
        while (1)
            ;
    }
}

void SysTick_Handler(void)
{
    s_tick++;
    lv_tick_inc(LVGL_TICK_MS);

    if ((s_tick % LVGL_TASK_PERIOD_TICK) == 0U)
    {
        s_lvglTaskPending = true;
    }
}

#if LV_BUILD_EXAMPLES

static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        lv_dropdown_get_selected_str(obj, buf, sizeof(buf));
        LV_LOG_USER("Option: %s", buf);
    }
}

void lv_example_dropdown_1(void)
{

    /*Create a normal drop down list*/
    lv_obj_t * dd = lv_dropdown_create(lv_scr_act());
    lv_dropdown_set_options(dd, "Apple\n"
                            "Banana\n"
                            "Orange\n"
                            "Cherry\n"
                            "Grape\n"
                            "Raspberry\n"
                            "Melon\n"
                            "Orange\n"
                            "Lemon\n"
                            "Nuts");

    lv_obj_align(dd, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_add_event_cb(dd, event_handler, LV_EVENT_ALL, NULL);
}
#endif

/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{

    /* TODO: add your own code here */
    fsp_err_t  err;
    /* Fill the Frame buffer with Blue, to zero out info from previous execution runs */
    uint32_t count;

    uint16_t * p = (uint16_t *)&fb_background[0][0];
    for (count = 0; count < sizeof(fb_background)/2; count++)
    {
        *p++ = RGB_565_REG;
    }

    DEMO_SetupTick(); //为lvgl提供心跳
    lv_init();
    lv_port_disp_init();
#if Touch_Enable
    lv_port_indev_init();
#endif

       lv_example_dropdown_1();
   #if LV_USE_DEMO_WIDGETS
//       lv_demo_widgets();
   #endif

   #if LV_USE_DEMO_STRESS
       lv_demo_stress();
   #endif
   #if LV_USE_DEMO_BENCHMARK
       lv_demo_benchmark();
   #endif
   #if LV_USE_DEMO_MUSIC
       lv_demo_music();
   #endif
   //    lv_task_handler();
       /* handle the tasks of LVGL */
       while(1)
       {
           while (!s_lvglTaskPending)
           {
           }
           s_lvglTaskPending = false;
           lv_task_handler();
       }



#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

/*******************************************************************************************************************//**
 * This function is called at various points during the startup process.  This implementation uses the event that is
 * called right before main() to set up the pins.
 *
 * @param[in]  event    Where at in the start up process the code is currently at
 **********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, &IOPORT_CFG_NAME);

        bsp_sdram_init(); //SDRAM pins need to be set to HIGH drive strength in pin configuration
    }
}

#if BSP_TZ_SECURE_BUILD

FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
FSP_CPP_FOOTER

#endif
